from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
class Base():
    def __init__(self,driver):
        self.driver=driver
    # 导入网址
    def open_url(self,url):
        self.driver.get(url)
    def error_png(self):
        error_png=time.strftime('%Y%m%d-%H%M%S')
        return error_png
    # 定位元素封装
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.driver.save_screenshot('../Png/'+self.error_png()+'.png')
            print(e)
    # 输入事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 点击事件
    # def click(self,loc):
    #     self.find_element(loc).click()
    # 滑动事件
    def execute_script(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')
    # 页面切换
    def switch_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # frame框架
    def switch_frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 键盘事件ctrl+a全选
    def Ctrl_A(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'a')
    # 键盘事件ctrl+c复制
    def Ctrl_C(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'c')
    # 键盘事件ctrl+v粘贴
    def Ctrl_V(self,loc):
        self.find_element(loc).send_keys(Keys.CONTROL,'v')
    # 鼠标事件单击
    def click(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()
    # 鼠标双击事件
    def doule_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    # 鼠标悬浮事件
    def move_to_element(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()